-- SPDX-License-Identifier: GPL-3.0-or-later

local extension = Package("ling1")
extension.extensionName = "lingling"

local U = require "packages/utility/utility"

local limuz = General(extension, "lingling__limuz", "zhao", 4)
local zhubei = fk.CreateTriggerSkill{
  name = "lingling__zhubei",
  anim_type = "drawcard",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      table.find(player.room.alive_players, function (p)
        return not table.contains(player:getTableMark("lingling__zhubei-turn"), p.id)
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return not table.contains(player:getTableMark("lingling__zhubei-turn"), p.id)
    end)
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
      "#lingling__zhubei-choose:::"..math.min(player:usedSkillTimes(self.name, Player.HistoryTurn) + 1, 3), self.name, true)
    if #to > 0 then
      self.cost_data = {tos = to}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data.tos[1])
    room:addTableMark(player, "lingling__zhubei-turn", to.id)
    to:drawCards(2, self.name)
    if to.dead then return end
    local n = math.min(player:usedSkillTimes(self.name, Player.HistoryTurn), 3)
    room:askForDiscard(to, n, n, true, self.name, false)
  end,
}
local qingye = fk.CreateViewAsSkill{
  name = "lingling__qingye",
  anim_type = "offensive",
  prompt = "#lingling__qingye",
  card_filter = function(self, to_select, selected)
    return table.contains(Self:getCardIds("h"), to_select) and #selected < Self:getHandcardNum() - 1
  end,
  view_as = function(self, cards)
    if #cards == 0 then return end
    self.cost_data = cards
    local card = Fk:cloneCard("fire_attack")
    card.skillName = self.name
    return card
  end,
  before_use = function (self, player, use)
    player:addToPile("lingling__qingye&", self.cost_data, false, self.name, player.id)
    if not player:isKongcheng() and not player.dead then
      player:showCards(player:getCardIds("h"))
    end
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  enabled_at_response = Util.FalseFunc,
}
local qingye_delay = fk.CreateTriggerSkill{
  name = "#lingling__qingye_delay",
  mute = true,
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and #player:getPile("lingling__qingye&") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:moveCardTo(player:getPile("lingling__qingye&"), Card.PlayerHand, player, fk.ReasonJustMove, "lingling__qingye")
  end,
}
qingye:addRelatedSkill(qingye_delay)
limuz:addSkill(zhubei)
limuz:addSkill(qingye)
Fk:loadTranslationTable{
  ["lingling__limuz"] = "李牧",
  ["#lingling__limuz"] = "天下之将",
  ["illustrator:lingling__limuz"] = "珊瑚虫",
  ["designer:lingling__limuz"] = "伶",

  ["lingling__zhubei"] = "逐北",
  [":lingling__zhubei"] = "每回合对每名角色限一次，当你使用牌结算后，你可以令一名角色摸两张牌，然后其弃置X张牌（X为此技能本回合发动次数，"..
  "至多为3）。",
  ["lingling__qingye"] = "清野",
  [":lingling__qingye"] = "出牌阶段限一次，你可以将任意张手牌扣置于武将牌上（至少留一张），然后展示所有手牌视为使用【火攻】，下回合开始时你获得"..
  "这些扣置牌，你可以将这些牌当手牌使用或打出。"..
  "<br><br> <font color = '#a40000'>良将身亡赵亦亡，百年遗恨一冯唐。",
  ["#lingling__zhubei-choose"] = "逐北：你可以令一名角色摸两张牌然后弃%arg张牌",
  ["#lingling__qingye"] = "清野：将任意张手牌扣置于武将牌上（至少留一张）、展示所有手牌，视为使用【火攻】",
  ["lingling__qingye&"] = "清野",
}

local linxiangru = General(extension, "lingling__linxiangru", "zhao", 3)
local huanbi = fk.CreateTriggerSkill{
  name = "lingling__huanbi",
  anim_type = "control",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
        if move.from and move.to and move.moveReason == fk.ReasonPrey and
          move.proposer == move.to and move.from ~= move.to and
          not player.room:getPlayerById(move.to).dead and
          player:distanceTo(player.room:getPlayerById(move.from)) < 2 then
          return true
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.from and move.to and move.moveReason == fk.ReasonPrey and
        move.proposer == move.to and move.from ~= move.to and
        not room:getPlayerById(move.to).dead and
        player:distanceTo(room:getPlayerById(move.from)) < 2 then
        table.insertIfNeed(targets, move.to)
      end
    end
    room:sortPlayersByAction(targets)
    for _, id in ipairs(targets) do
      if not player:hasSkill(self) or player:usedSkillTimes(self.name, Player.HistoryTurn) > 0 then break end
      local p = room:getPlayerById(id)
      if not p.dead then
        self:doCost(event, p, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, self.name, nil, "#lingling__huanbi-invoke::"..target.id) then
      self.cost_data = {tos = {target.id}}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(target, {"lingling__huanbi1:"..player.id, "lingling__huanbi2:"..player.id}, self.name)
    if choice[17] == "1" then
      player:drawCards(1, self.name)
      if target.dead then return end
      local moves = {}
      for _, move in ipairs(data) do
        if move.from and move.to == target.id and move.moveReason == fk.ReasonPrey and
          move.proposer == move.to and move.from ~= move.to and
          not room:getPlayerById(move.from).dead then
          local ids = {}
          for _, info in ipairs(move.moveInfo) do
            if table.contains(target:getCardIds("h"), info.cardId) then
              table.insertIfNeed(ids, info.cardId)
            end
          end
          if #ids > 0 then
            table.insert(moves, {
              ids = ids,
              from = move.to,
              to = move.from,
              toArea = Card.PlayerHand,
              moveReason = fk.ReasonGive,
              skillName = self.name,
              proposer = move.to,
              moveVisible = false,
            })
          end
        end
      end
      if #moves > 0 then
        room:moveCards(table.unpack(moves))
      end
    else
      room:askForDiscard(player, 1, 1, true, self.name)
      if player.dead or target.dead or target:isNude() then return end
      if target == player then
        room:askForDiscard(player, 3, 3, true, self.name, false)
      else
        local cards = room:askForCardsChosen(player, target, 3, 3, "he", self.name, "#lingling__huanbi-discard::"..target.id)
        room:throwCard(cards, self.name, target, player)
      end
    end
  end,
}
local zhengci = fk.CreateTriggerSkill{
  name = "lingling__zhengci",
  anim_type = "control",
  events = {fk.Damage, fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.Damage then
        return target and player.room.logic:getActualDamageEvents(1, function(e)
          return e.data[1].from == target
        end)[1].data[1] == data and
          target.hp > data.to.hp and not target.dead and not data.to.dead and not data.to:isNude()
      elseif event == fk.DrawNCards then
        return target == player and player:getMark("lingling__zhengci-turn") == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      return room:askForSkillInvoke(target, self.name, nil, "#lingling__zhengci-invoke::"..data.to.id)
    elseif event == fk.DrawNCards then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damage then
      room:doIndicate(target.id, {data.to.id})
      local card = room:askForCardChosen(target, data.to, "he", self.name, "#lingling__zhengci-prey::"..data.to.id)
      room:moveCardTo(card, Card.PlayerHand, target, fk.ReasonPrey, self.name, nil, false, target.id)
    elseif event == fk.DrawNCards then
      data.n = data.n + 2
      room:setPlayerMark(player, "lingling__zhengci_used", 1)
    end
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("lingling__zhengci_used") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "lingling__zhengci_used", 0)
    room:setPlayerMark(player, "lingling__zhengci-turn", 1)
  end,
}
linxiangru:addSkill(huanbi)
linxiangru:addSkill(zhengci)
Fk:loadTranslationTable{
  ["lingling__linxiangru"] = "蔺相如",
  ["#lingling__linxiangru"] = "凛凛死义",
  ["illustrator:lingling__linxiangru"] = "珊瑚虫",
  ["designer:lingling__linxiangru"] = "伶",

  ["lingling__huanbi"] = "还壁",
  [":lingling__huanbi"] = "每回合限一次，当一名角色获得另一名你与其距离为1以内的角色的牌后，你可以令获得牌的角色选择：你摸一张牌，"..
  "然后其交还获得的牌；你弃置一张牌（无牌则不弃），然后弃置其三张牌。",
  ["lingling__zhengci"] = "正辞",
  [":lingling__zhengci"] = "任意角色每回合首次造成伤害后，若其体力大于受到伤害的角色，其可以获得该角色一张牌。摸牌阶段，若你上回合未如此做，"..
  "你多摸两张牌。"..
  "<br><br> <font color = '#a40000'>最怜恃勇偏轻举，直挟君王冒虎狼。",
  ["#lingling__huanbi-invoke"] = "还壁：是否对 %dest 发动“还壁”，令其选择一项？",
  ["lingling__huanbi1"] = "%src 摸一张牌，你交还你获得的牌",
  ["lingling__huanbi2"] = "%src 弃一张牌，弃置你三张牌",
  ["#lingling__huanbi-discard"] = "还壁：弃置 %dest 三张牌",
  ["#lingling__zhengci-invoke"] = "正辞：是否获得 %dest 一张牌？",
  ["#lingling__zhengci-prey"] = "正辞：获得 %dest 的一张牌",
}

local sunbin = General(extension, "lingling__sunbin", "qi", 3)
local jianzao = fk.CreateTriggerSkill{
  name = "lingling__jianzao",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name)
    if player.dead then return end
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event and turn_event.data[1] == player then
      room:addPlayerMark(player, MarkEnum.SlashResidue.."-turn", 1)
    elseif not player:isNude() then
      U.askForUseRealCard(room, player, player:getCardIds("h&"), ".|.|.|.|.|equip", self.name,
        "#lingling__jianzao-use", nil, false, true)
    end
  end,
}
local weiwei = fk.CreateViewAsSkill{
  name = "lingling__weiwei",
  anim_type = "control",
  pattern = "unexpectation,nullification",
  prompt = "#lingling__weiwei",
  card_filter = function(self, to_select, selected)
    if #selected == 1 then return false end
    local c = Fk:getCardById(to_select)
    local card
    if c.color == Card.Red then
      card = Fk:cloneCard("unexpectation")
    elseif c.color == Card.Black then
      card = Fk:cloneCard("nullification")
    else
      return false
    end
    return (Fk.currentResponsePattern == nil and Self:canUse(card)) or
      (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local c = Fk:getCardById(cards[1])
    local card
    if c.color == Card.Red then
      card = Fk:cloneCard("unexpectation")
    elseif c.color == Card.Black then
      card = Fk:cloneCard("nullification")
    end
    card.skillName = self.name
    card:addSubcard(cards[1])
    return card
  end,
  before_use = function (self, player, use)
    player.room:setPlayerMark(player, "lingling__weiwei_"..use.card.name.."-turn", 1)
  end,
  after_use = function (self, player, use)
    local room = player.room
    if player.dead or not use.damageDealt or use.card.name ~= "unexpectation" then return end
    local targets = {}
    for _, id in ipairs(TargetGroup:getRealTargets(use.tos)) do
      local p = room:getPlayerById(id)
      local start_id, end_id = 1, 1
      room.logic:getEventsByRule(GameEvent.Turn, 1, function (e)
        if e.data[1] == p then
          start_id, end_id = e.id, e.end_id
          return true
        end
      end, 1)
      if start_id > 1 then
        room.logic:getEventsByRule(GameEvent.Damage, 1, function (e)
          if e.id <= end_id then
            table.insertIfNeed(targets, e.data[1].to)
          end
        end, start_id)
      end
    end
    targets = table.filter(targets, function (p)
      return p:isWounded() and not p.dead
    end)
    if #targets > 0 then
      local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
        "#lingling__weiwei-recover", self.name, true)
      if #to > 0 then
        room:recover({
          who = room:getPlayerById(to[1]),
          num = 1,
          recoverBy = player,
          skillName = self.name,
        })
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:getMark("lingling__weiwei_unexpectation-turn") == 0
  end,
  enabled_at_response = function (self, player, response)
    if response or player:isNude() then return end
    for _, name in ipairs({"unexpectation", "nullification"}) do
      if player:getMark("lingling__weiwei_"..name.."-turn") == 0 then
        local card = Fk:cloneCard(name)
        card.skillName = self.name
        if (Fk.currentResponsePattern == nil and player:canUse(card)) or
          (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
          return true
        end
      end
    end
  end,
}
sunbin:addSkill(jianzao)
sunbin:addSkill(weiwei)
Fk:loadTranslationTable{
  ["lingling__sunbin"] = "孙膑",
  ["#lingling__sunbin"] = "兵机莫测",
  ["illustrator:lingling__sunbin"] = "珊瑚虫",
  ["designer:lingling__sunbin"] = "伶",

  ["lingling__jianzao"] = "减灶",
  [":lingling__jianzao"] = "当你失去装备区的牌后，你可以摸两张牌，若为你回合内，你本回合使用【杀】的次数上限+1，若为你回合外，"..
  "你可以使用一张装备牌。",
  ["lingling__weiwei"] = "围魏",
  [":lingling__weiwei"] = "每回合各限一次，你可以将红色牌当【出其不意】、黑色牌当【无懈可击】使用，若【出其不意】造成伤害，你可以令一名被目标"..
  "上回合造成伤害的角色回复1点体力。"..
  "<br><br> <font color = '#a40000'>百年家学妙兵机，知彼犹怜己未知。",
  ["#lingling__jianzao-use"] = "减灶：你可以使用一张装备牌",
  ["#lingling__weiwei"] = "围魏：你可以将红色牌当【出其不意】、黑色牌当【无懈可击】使用",
  ["#lingling__weiwei-recover"] = "围魏：你可以令其中一名角色回复1点体力",
}

local yueyi = General(extension, "lingling__yueyi", "yan", 4)
local lianke = fk.CreateActiveSkill{
  name = "lingling__lianke",
  anim_type = "control",
  card_num = 0,
  target_num = 0,
  prompt = "#lingling__lianke",
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:drawCards(2, self.name)
    if player.dead or player:isNude() then return end
    local card = Fk:cloneCard("dismantlement")
    card.skillName = self.name
    if not table.find(room:getOtherPlayers(player), function (p)
        return player:canUseTo(card, p)
      end) then return end
    local success, dat = room:askForUseActiveSkill(player, "lingling__lianke_viewas", "#lingling__lianke-dismantlement", false)
    if success and dat then
      card = Fk.skills["lingling__lianke_viewas"]:viewAs(dat.cards)
      room:useCard{
        from = player.id,
        tos = table.map(dat.targets, function(id) return { id } end),
        card = card,
      }
    end
  end,
}
local lianke_viewas = fk.CreateViewAsSkill{
  name = "lingling__lianke_viewas",
  card_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local card = Fk:cloneCard("dismantlement")
    card.skillName = "lingling__lianke"
    card:addSubcard(cards[1])
    return card
  end,
}
local lianke_delay = fk.CreateTriggerSkill{
  name = "#lingling__lianke_delay",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player.dead and not player:isNude() then
      for _, move in ipairs(data) do
        if move.from and move.moveReason == fk.ReasonDiscard and move.skillName == "dismantlement_skill" and
          move.proposer and move.proposer == player.id and
          not player.room:getPlayerById(move.from).dead then
          local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use_event ~= nil then
            local use = use_event.data[1]
            if table.contains(use.card.skillNames, "lingling__lianke") then
              for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId).color == Card.Red then
                  return true
                end
              end
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    if not player.dead and not player:isNude() then
      for _, move in ipairs(data) do
        if move.from and move.moveReason == fk.ReasonDiscard and move.skillName == "dismantlement_skill" and
          move.proposer and move.proposer == player.id then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use_event ~= nil then
            local use = use_event.data[1]
            if table.contains(use.card.skillNames, "lingling__lianke") then
              for _, info in ipairs(move.moveInfo) do
                if Fk:getCardById(info.cardId).color == Card.Red then
                  table.insertIfNeed(targets, move.from)
                end
              end
            end
          end
        end
      end
    end
    room:sortPlayersByAction(targets)
    for _, id in ipairs(targets) do
      if player.dead or player:isNude() then break end
      local p = room:getPlayerById(id)
      if not p.dead then
        self:doCost(event, p, player, data)
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    while not player.dead and not target.dead and not player:isNude() do
      local cards = table.filter(player:getCardIds("he&"), function (id)
        if Fk:getCardById(id).color == Card.Black then
          local card = Fk:cloneCard("slash")
          card.skillName = "lingling__lianke"
          card:addSubcard(id)
          return player:canUseTo(card, target, {bypass_distances = true, bypass_times = true})
        end
      end)
      local card = room:askForCard(player, 1, 1, true, "lingling__lianke", true, tostring(Exppattern{ id = cards }),
        "#lingling__lianke-slash::"..target.id)
      if #card > 0 then
        local use = room:useVirtualCard("slash", card, player, target, "lingling__lianke", true)
        if use.damageDealt then
          break
        end
      else
        break
      end
    end
  end,
}
local bugong = fk.CreateTriggerSkill{
  name = "lingling__bugong",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to.hp == 1 and
      not table.contains(player:getTableMark("lingling__bugong-turn"), data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event and turn_event.data[1] == player then
      room:addTableMark(player, self.name, data.to.id)
    end
    player:drawCards(2, self.name)
    if turn_event then
      room:addTableMark(data.to, "@@lingling__bugong-turn", player.id)
    end
    return true
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark(self.name) ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "lingling__bugong-turn", player:getMark(self.name))
    room:setPlayerMark(player, self.name, 0)
  end,
}
local bugong_prohibit = fk.CreateProhibitSkill{
  name = "#lingling__bugong_prohibit",
  is_prohibited = function(self, from, to, card)
    return card and table.contains(to:getTableMark("@@lingling__bugong-turn"), from.id)
  end,
}
Fk:addSkill(lianke_viewas)
lianke:addRelatedSkill(lianke_delay)
bugong:addRelatedSkill(bugong_prohibit)
yueyi:addSkill(lianke)
yueyi:addSkill(bugong)
Fk:loadTranslationTable{
  ["lingling__yueyi"] = "乐毅",
  ["#lingling__yueyi"] = "芳规不渝",
  ["illustrator:lingling__yueyi"] = "珊瑚虫",
  ["designer:lingling__yueyi"] = "伶",

  ["lingling__lianke"] = "连克",
  [":lingling__lianke"] = "出牌阶段限一次，你可以摸两张牌，然后将一张牌当【过河拆桥】使用，若弃置红色牌，你可以将一张黑色牌当无次数限制的"..
  "【杀】对其使用，若未造成伤害，你可以重复此流程。",
  ["lingling__bugong"] = "不攻",
  [":lingling__bugong"] = "当你对体力为1的角色造成伤害时，若你上回合未对其触发过此技能，你防止此伤害并摸两张牌，然后本回合不能对其使用牌。"..
  "<br><br> <font color = '#a40000'>七十城收一笑间，当时气势擅强燕。",
  ["#lingling__lianke"] = "连克：你可以摸两张牌，然后将一张牌当【过河拆桥】使用",
  ["lingling__lianke_viewas"] = "连克",
  ["#lingling__lianke-dismantlement"] = "连克：请将一张牌当【过河拆桥】使用",
  ["#lingling__lianke-slash"] = "连克：是否将一张黑色牌当【杀】对 %dest 使用？",
  ["@@lingling__bugong-turn"] = "不攻",
}

local quyuan = General(extension, "lingling__quyuan", "chu", 3)
local yuanci = fk.CreateTriggerSkill{
  name = "lingling__yuanci",
  anim_type = "control",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not target.dead and
      #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        return use.from == target.id and use.card.trueName == "slash"
      end, Player.HistoryTurn) == 0
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#lingling__yuanci-invoke::"..target.id)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    if player.dead or target.dead or target:isAllNude() or target == player then return end
    room:useVirtualCard("sincere_treat", nil, player, target, self.name)
    if player.dead or target.dead or target:isNude() then return end
    local card = room:askForCard(target, 1, 1, false, self.name, true, "slash", "#lingling__yuanci-show:"..player.id)
    if #card > 0 then
      target:showCards(card)
      if player.dead or target.dead or player:isAllNude() then return end
      room:useVirtualCard("dismantlement", nil, target, player, self.name)
    end
  end,
}
local qiusuo = fk.CreateTriggerSkill{
  name = "lingling__qiusuo",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.GameStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills1, skills2 = {}, {}
    for name, skill in pairs(Fk.skills) do
      if skill ~= self and not skill.attached_equip and not name:endsWith("&") and not name:startsWith("#") and
        string.find(Fk:translate(":"..name, "zh_CN"), "上回合") then
        table.insertIfNeed(skills1, name)
      end
    end
    for name, skill in pairs(Fk.skills) do
      if skill ~= self and not skill.attached_equip and not name:endsWith("&") and not name:startsWith("#") and
        string.find(Fk:translate(":"..name, "zh_CN"), "下回合") then
        table.insertIfNeed(skills2, name)
      end
    end
    local skills = {}
    if #skills1 > 0 then
      table.insert(skills, table.random(skills1))
    end
    if #skills2 > 0 then
      for i = 1, 20, 1 do
        local skill = table.random(skills2)
        if not table.contains(skills, skill) then
          table.insert(skills, skill)
          break
        end
      end
    end
    if #skills > 0 then
      room:handleAddLoseSkills(player, skills, nil, true, false)
    end
  end,
}
quyuan:addSkill(yuanci)
quyuan:addSkill(qiusuo)
Fk:loadTranslationTable{
  ["lingling__quyuan"] = "屈原",
  ["#lingling__quyuan"] = "词悬日月",
  ["illustrator:lingling__quyuan"] = "珊瑚虫",
  ["designer:lingling__quyuan"] = "伶",

  ["lingling__yuanci"] = "怨辞",
  [":lingling__yuanci"] = "本回合未使用过【杀】的角色回合结束时，你可以摸一张牌视为对其使用【推心置腹】，然后其可以展示一张【杀】视为对你使用"..
  "【过河拆桥】。",
  ["lingling__qiusuo"] = "求索",
  [":lingling__qiusuo"] = "游戏开始时，你随机获得一个描述含“上回合”的技能，和一个描述含“下回合”的技能。"..
  "<br><br> <font color = '#a40000'>长太息以掩涕兮，哀民生之多艰。",
  ["#lingling__yuanci-invoke"] = "怨辞：是否摸一张牌，视为对 %dest 使用【推心置腹】？",
  ["#lingling__yuanci-show"] = "怨辞：是否展示一张【杀】，视为对 %src 使用【过河拆桥】？",
}

local tianwen = General(extension, "lingling__tianwen", "qi", 3)
local jiaogong = fk.CreateActiveSkill{
  name = "lingling__jiaogong",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__jiaogong",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0 then
      if table.find(Fk:currentRoom().alive_players, function (p)
        return p ~= Self and not Self:inMyAttackRange(p)
      end) then
        return to_select ~= Self.id and not Self:inMyAttackRange(Fk:currentRoom():getPlayerById(to_select))
      else
        return to_select == Self.id
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    player:drawCards(1, self.name)
    if not target.dead then
      target:drawCards(1, self.name)
    end
    local card1, card2
    if not player.dead then
      card1 = room:askForDiscard(player, 1, 1, true, self.name, false)
    end
    if not target.dead then
      card2 = room:askForDiscard(target, 1, 1, true, self.name, false)
    end
    if card1 and card2 and Fk:getCardById(card1[1]):compareSuitWith(Fk:getCardById(card2[1])) and not player.dead then
      local targets = table.filter(room.alive_players, function (p)
        return player:inMyAttackRange(p)
      end)
      if #targets > 0 then
        local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1,
          "#lingling__jiaogong-choose", self.name, true)
        if #to > 0 then
          room:damage{
            from = player,
            to = room:getPlayerById(to[1]),
            damage = 1,
            skillName = self.name,
          }
        end
      end
    end
  end,
}
local sanku = fk.CreateTriggerSkill{
  name = "lingling__sanku",
  anim_type = "masochism",
  frequency = Skill.Compulsory,
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target and data.to == player then
      local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      return turn_event and turn_event.data[1] == target
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    if player.dead or player:usedSkillTimes(self.name, Player.HistoryTurn) > 1 then return end
    local start_id, end_id = 1, 1
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if not turn_event then return end
    local start_id2, end_id2 = turn_event.id, turn_event.end_id or (room.logic:getCurrentEvent().id + 1)
    room.logic:getEventsByRule(GameEvent.Turn, 1, function (e)
      if e.data[1] == target and e.id < start_id2 then
        start_id, end_id = e.id, e.end_id
        return true
      end
    end, 1)
    if start_id > 1 then
      if #room.logic:getEventsByRule(GameEvent.SkillEffect, 1, function (e)
        if (e.id >= start_id2 and e.id <= end_id2) or (e.id >= start_id and e.id <= end_id) then
          return e.data[3] == self and e.data[2] == player
        end
      end, start_id) == 0 then
        player:drawCards(2, self.name)
      end
    end
  end,
}
tianwen:addSkill(jiaogong)
tianwen:addSkill(sanku)
Fk:loadTranslationTable{
  ["lingling__tianwen"] = "田文",
  ["#lingling__tianwen"] = "飞仁扬义",
  ["illustrator:lingling__tianwen"] = "珊瑚虫",
  ["designer:lingling__tianwen"] = "伶",

  ["lingling__jiaogong"] = "交攻",
  [":lingling__jiaogong"] = "出牌阶段限一次，你可以与（一名攻击范围外的其他角色）各摸一张牌，然后你可以与其各弃置一张牌，若花色相同，"..
  "你可以对一名攻击范围内的角色造成1点伤害。若没有符合括号内条件的角色，则括号内改为“你”。",
  ["lingling__sanku"] = "三窟",
  [":lingling__sanku"] = "当其他角色于回合内对你造成伤害后，你摸一张牌，若为其上回合开始至今首次触发此技能，你再摸两张牌。"..
  "<br><br> <font color = '#a40000'>狗盗鸡鸣却遇知，可怜真士不逢时。",
  ["#lingling__jiaogong"] = "交攻：与一名角色各摸一张牌并各弃一张牌，若花色相同则可以对攻击范围内一名角色造成1点伤害",
  ["#lingling__jiaogong-choose"] = "交攻：你可以对攻击范围内一名角色造成1点伤害",
}

local zhaosheng = General(extension, "lingling__zhaosheng", "zhao", 4)
local xiangshi = fk.CreateTriggerSkill{
  name = "lingling__xiangshi",
  anim_type = "control",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 10,
      "#lingling__xiangshi-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = {tos = tos}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(#self.cost_data.tos, self.name)
    if player.dead or player:isNude() then return end
    local targets = table.filter(self.cost_data.tos, function (id)
      return not room:getPlayerById(id).dead
    end)
    if #targets == 0 then return end
    local result = room:askForYiji(player, player:getCardIds("he"), table.map(targets, Util.Id2PlayerMapper), self.name,
      #targets, #targets, "#lingling__xiangshi-give", nil, false, 1)
    if player.dead then return end
    for id, ids in pairs(result) do
      if table.find(ids, function (i)
        return Fk:getCardById(i).type == Card.TypeEquip
      end) then
        room:setPlayerMark(room:getPlayerById(id), "@@lingling__xiangshi", 1)
        room:addTableMark(player, self.name, id)
      end
    end
  end,
}
local xiangshi_delay = fk.CreateTriggerSkill{
  name = "#lingling__xiangshi_delay",
  mute = true,
  events = {fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    return data.card.trueName == "slash" and player:getMark("lingling__xiangshi_invoke-turn") == target.id
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("lingling__xiangshi")
    room:notifySkillInvoked(player, "lingling__xiangshi", "control")
    room:setPlayerMark(player, "lingling__xiangshi-tmp", {TargetGroup:getRealTargets(data.tos), room:getUseExtraTargets(data, true)})
    local success, dat = room:askForUseActiveSkill(player, "lingling__xiangshi_active",
      "#lingling__xiangshi-invoke::"..target.id..":"..data.card:toLogString(), true, nil, false)
    room:setPlayerMark(player, "lingling__xiangshi-tmp", 0)
    if success and dat then
      if table.contains(TargetGroup:getRealTargets(data.tos), dat.targets[1]) then
        for _, id in ipairs(dat.targets) do
          TargetGroup:removeTarget(data.tos, id)
        end
      else
        data.tos = {}
        for _, id in ipairs(dat.targets) do
          table.insert(data.tos, {id})
        end
      end
    end
  end,

  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@lingling__xiangshi") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@@lingling__xiangshi", 0)
    room:setPlayerMark(player, "@@lingling__xiangshi-turn", 1)
    for _, p in ipairs(room.alive_players) do
      if table.contains(p:getTableMark("lingling__xiangshi"), player.id) then
        room:removeTableMark(p, "lingling__xiangshi", player.id)
        room:setPlayerMark(p, "lingling__xiangshi_invoke-turn", player.id)
      end
    end
  end,
}
local xiangshi_active = fk.CreateActiveSkill{
  name = "lingling__xiangshi_active",
  card_num = 0,
  min_target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected)
    local mark = Self:getMark("lingling__xiangshi-tmp")
    if table.contains(mark[1], to_select) or table.contains(mark[2], to_select) then
      if #selected == 0 then
        return true
      else
        if table.contains(mark[1], selected[1]) then
          return table.contains(mark[1], to_select)
        else
          return #selected < #mark[1] and table.contains(mark[2], to_select)
        end
      end
    end
  end,
  target_tip = function(self, to_select, selected, selected_cards, card, selectable, extra_data)
    local mark = Self:getMark("lingling__xiangshi-tmp")
    local jink = table.find(Fk:currentRoom():getPlayerById(to_select):getCardIds("h"), function (id)
      return Fk:getCardById(id).trueName == "jink"
    end) and {content = "lingling__xiangshi_jink", type = "normal"} or {content = "lingling__xiangshi_nojink", type = "warning"}
    if table.contains(mark[1], to_select) then
      return { {content = "@@CancelTarget", type = "warning"}, jink }
    elseif table.contains(mark[2], to_select) then
      return { {content = "@@ChangeTarget", type = "normal"}, jink }
    else
      return { jink }
    end
  end,
}
local xiangshi_targetmod = fk.CreateTargetModSkill{
  name = "#lingling__xiangshi_targetmod",
  bypass_distances =  function(self, player, skill)
    return skill.trueName == "slash_skill" and player:getMark("@@lingling__xiangshi-turn") > 0
  end,
}
local shoudang = fk.CreateActiveSkill{
  name = "lingling__shoudang",
  anim_type = "drawcard",
  frequency = Skill.Limited,
  min_card_num = 1,
  max_card_num = 4,
  target_num = 0,
  prompt = "#lingling__shoudang",
  expand_pile = function()
    return table.filter(Fk:currentRoom().discard_pile, function (id)
      return Fk:getCardById(id).type == Card.TypeEquip
    end)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and
      table.find(Fk:currentRoom().discard_pile, function (id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end)
  end,
  card_filter = function (self, to_select, selected)
    return #selected < 4 and table.contains(Fk:currentRoom().discard_pile, to_select)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:moveCardTo(effect.cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id)
  end,
}
Fk:addSkill(xiangshi_active)
xiangshi:addRelatedSkill(xiangshi_delay)
xiangshi:addRelatedSkill(xiangshi_targetmod)
zhaosheng:addSkill(xiangshi)
zhaosheng:addSkill(shoudang)
Fk:loadTranslationTable{
  ["lingling__zhaosheng"] = "赵胜",
  ["#lingling__zhaosheng"] = "天下奇器",
  ["illustrator:lingling__zhaosheng"] = "珊瑚虫",
  ["designer:lingling__zhaosheng"] = "伶",

  ["lingling__xiangshi"] = "飨士",
  [":lingling__xiangshi"] = "回合结束时，你可以指定任意名角色，你摸等量牌，然后交给这些角色各一张牌，因此获得装备牌的角色下回合"..
  "使用【杀】无距离限制，且使用时你得知所有角色手牌中有无【闪】，你选择替其重新指定目标或取消目标。",
  ["lingling__shoudang"] = "收党",
  [":lingling__shoudang"] = "限定技，出牌阶段，你可以从弃牌堆获得至多四张装备牌。"..
  "<br><br> <font color = '#a40000'>何事邯郸贵公子，能因牴者杀佳人。",
  ["#lingling__xiangshi-choose"] = "飨士：指定任意名角色，你摸等量牌，然后交给这些角色各一张牌",
  ["#lingling__xiangshi-give"] = "飨士：交给这些角色各一张牌，获得装备牌的角色下回合使用【杀】受你控制！",
  ["@@lingling__xiangshi"] = "飨士",
  ["@@lingling__xiangshi-turn"] = "飨士",
  ["#lingling__xiangshi_delay"] = "飨士",
  ["lingling__xiangshi_active"] = "飨士",
  ["#lingling__xiangshi-invoke"] = "飨士：%dest 使用%arg，你可以重新指定目标或取消目标！",
  ["@@ChangeTarget"] = "改变目标",
  ["lingling__xiangshi_jink"] = "有闪",
  ["lingling__xiangshi_nojink"] = "没有闪",
  ["#lingling__shoudang"] = "收党：你可以从弃牌堆获得至多四张装备牌！",
}

local huangxie = General(extension, "lingling__huangxie", "chu", 4)
local yihua = fk.CreateActiveSkill{
  name = "lingling__yihua",
  anim_type = "control",
  card_num = 0,
  target_num = 2,
  prompt = "#lingling__yihua",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, to_select, selected)
    local target = Fk:currentRoom():getPlayerById(to_select)
    if #selected == 0 then
      return true
    elseif #selected == 1 then
      return Fk:currentRoom():getPlayerById(selected[1]):canMoveCardsInBoardTo(target, nil)
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local result = room:askForMoveCardInBoard(player, room:getPlayerById(effect.tos[1]), room:getPlayerById(effect.tos[2]), self.name)
    if player.dead then return end
    local target = room:getPlayerById(result.from)
    if target ~= player then
      local use = room:useVirtualCard("slash", nil, target, player, self.name, true)
      if not player.dead and use.damageDealt and use.damageDealt[player.id] then
        player:drawCards(2, self.name)
      end
    end
  end,
}
local yihua_delay = fk.CreateTriggerSkill{
  name = "#lingling__yihua_delay",

  refresh_events = {fk.CardEffectCancelledOut},
  can_refresh = function(self, event, target, player, data)
    return data.card.trueName == "slash" and data.to == player.id and table.contains(data.card.skillNames, "lingling__yihua")
  end,
  on_refresh = function(self, event, target, player, data)
    player:setSkillUseHistory("lingling__yihua", 0, Player.HistoryPhase)
  end,
}
local yangdao = fk.CreateTriggerSkill{
  name = "lingling__yangdao",
  switch_skill_name = "lingling__yangdao",
  anim_type = "switch",
  frequency = Skill.Compulsory,
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
        return table.find(player.room.discard_pile, function (id)
          return Fk:getCardById(id).trueName == "jink"
        end)
      else
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(self.name, true) == fk.SwitchYang then
      local card = room:getCardsFromPileByRule("jink", 1, "discardPile")
      if #card > 0 then
        room:setPlayerMark(player, self.name, card[1])
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, true, player.id, "@@lingling__yangdao-inhand")
      end
    else
      if table.find(player:getCardIds("h"), function (id)
        return Fk:getCardById(id):getMark("@@lingling__yangdao-inhand") > 0
      end) then
        local success, dat = room:askForUseActiveSkill(player, "lingling__yangdao_viewas", "#lingling__yangdao-yin", true)
        if success and dat then
          local card = Fk.skills["lingling__yangdao_viewas"]:viewAs(dat.cards)
          room:useCard{
            from = player.id,
            tos = table.map(dat.targets, function(id) return {id} end),
            card = card,
          }
        end
      elseif not table.contains(player:getCardIds("h"), player:getMark(self.name)) then
        room:moveCardTo(player:getMark(self.name), Card.PlayerHand, player, fk.ReasonPrey, self.name, nil, true, player.id)
      end
    end
  end,
}
local yangdao_viewas = fk.CreateViewAsSkill{
  name = "lingling__yangdao_viewas",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select):getMark("@@lingling__yangdao-inhand") > 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("snatch")
    card.skillName = "lingling__yangdao"
    card:addSubcard(cards[1])
    return card
  end,
}
Fk:addSkill(yangdao_viewas)
yihua:addRelatedSkill(yihua_delay)
huangxie:addSkill(yihua)
huangxie:addSkill(yangdao)
Fk:loadTranslationTable{
  ["lingling__huangxie"] = "黄歇",
  ["#lingling__huangxie"] = "以身徇君",
  ["illustrator:lingling__huangxie"] = "珊瑚虫",
  ["designer:lingling__huangxie"] = "伶",

  ["lingling__yihua"] = "移花",
  [":lingling__yihua"] = "出牌阶段限一次，你可以移动场上一张牌，然后失去牌的角色视为对你使用【杀】，若被【闪】抵消，重置此技能，若对你造成伤害，"..
  "你摸两张牌。",
  ["lingling__yangdao"] = "养盗",
  [":lingling__yangdao"] = "转换技，①回合开始时，你从弃牌堆获得一张【闪】；②回合开始时，若你上次因①获得的【闪】仍在手牌，你可以将之"..
  "当【顺手牵羊】使用，若不在，你获得之（无论它在哪）。"..
  "<br><br> <font color = '#a40000'>志大下之奇货者，必中天一卜之奇祸。",
  ["#lingling__yihua"] = "移花：你可以移动场上一张牌，失去牌的角色视为对你使用【杀】",
  ["@@lingling__yangdao-inhand"] = "养盗",
  ["#lingling__yangdao-yin"] = "养盗：你可以将“养盗”【闪】当【顺手牵羊】使用",
}

local weiwuji = General(extension, "lingling__weiwuji", "wei", 4)
local huimei = fk.CreateActiveSkill{
  name = "lingling__huimei",
  anim_type = "control",
  prompt = function (self)
    return "#"..self.interaction.data
  end,
  card_num = 0,
  target_num = 1,
  interaction = function()
    return UI.ComboBox {choices = {"lingling__huimei1", "lingling__huimei2"}}
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= Self.id and not table.contains(Self:getTableMark(self.name), to_select) then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if self.interaction.data == "lingling__huimei1" then
        return Self:getHandcardNum() > target:getHandcardNum() and
          not (#Self:getCardIds("e") == 0 and #target:getCardIds("e") == 0)
      else
        return #Self:getCardIds("e") > #target:getCardIds("e") and
          not (Self:isKongcheng() and target:isKongcheng())
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local n = 0
    if self.interaction.data == "lingling__huimei1" then
      n = #player:getCardIds("e")
      U.swapCards(room, player, player, target, player:getCardIds("e"), target:getCardIds("e"), "lingling__huimei", Card.PlayerEquip)
      n = #player:getCardIds("e") - n
    else
      n = player:getHandcardNum()
      U.swapHandCards(room, player, player, target, "lingling__huimei")
      n = player:getHandcardNum() - n
    end
    if not player.dead and n > 1 then
      room:loseHp(player, 1, self.name)
    end
    if not player.dead and n > 3 then
      room:addTableMark(player, self.name, target.id)
    end
  end,
}
local shiwei = fk.CreateActiveSkill{
  name = "lingling__shiwei",
  anim_type = "control",
  frequency = Skill.Limited,
  prompt = "#lingling__shiwei",
  card_num = 0,
  target_num = 1,
  interaction = function()
    return UI.ComboBox {choices = {"lingling__huimei1", "lingling__huimei2"}}
  end,
  can_use = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= Self.id then
      local target = Fk:currentRoom():getPlayerById(to_select)
      if self.interaction.data == "lingling__huimei1" then
        return not (#Self:getCardIds("e") == 0 and #target:getCardIds("e") == 0)
      else
        return not (Self:isKongcheng() and target:isKongcheng())
      end
    end
  end,
  on_use = function(self, room, effect)
    huimei.interaction = huimei.interaction or {}
    huimei.interaction.data = self.interaction.data
    huimei:onUse(room, {
      from = effect.from,
      tos = effect.tos,
    })
  end,
}
weiwuji:addSkill(huimei)
weiwuji:addSkill(shiwei)
Fk:loadTranslationTable{
  ["lingling__weiwuji"] = "魏无忌",
  ["#lingling__weiwuji"] = "九野生风",
  ["illustrator:lingling__weiwuji"] = "珊瑚虫",
  ["designer:lingling__weiwuji"] = "易大剧",

  ["lingling__huimei"] = "挥袂",
  [":lingling__huimei"] = "出牌阶段限一次，你可以与一名手牌少于你的角色交换装备区所有牌，或与一名装备区牌少于你的角色交换所有手牌，然后若你总牌数"..
  "增加了：至少两张，你失去1点体力；至少四张，你不能再对其发动“挥袂”。",
  ["lingling__shiwei"] = "势危",
  [":lingling__shiwei"] = "限定技，你可以无视前置条件限制发动“挥袂”。"..
  "<br><br> <font color = '#a40000'>邯郸应与赋同袍，公子翩翩气自豪。",
  ["#lingling__huimei1"] = "挥袂：你可以与一名手牌数少于你的角色交换装备",
  ["#lingling__huimei2"] = "挥袂：你可以与一名装备数少于你的角色交换手牌",
  ["lingling__huimei1"] = "交换装备",
  ["lingling__huimei2"] = "交换手牌",
  ["#lingling__shiwei"] = "势危：你可以无视条件限制发动“挥袂”！",
}

local goujian = General(extension, "lingling__goujian", "qun", 3)
local fenzhi = fk.CreateActiveSkill{
  name = "lingling__fenzhi",
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#lingling__fenzhi",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local target = room:getPlayerById(effect.tos[1])
    target:drawCards(2, self.name)
    if target.dead or target:isKongcheng() then return end
    local card = room:askForCard(target, 1, 1, false, self.name, false, nil, "#lingling__fenzhi-ask")
    room:setCardMark(Fk:getCardById(card[1]), "lingling__fenzhi-inhand", 1)
    room:setPlayerMark(target, self.name, 1)
    room:setPlayerMark(target, "lingling__fenzhi-turn", 1)
    target:filterHandcards()
  end,
}
local fenzhi_delay = fk.CreateTriggerSkill{
  name = "#lingling__daishi_delay",

  refresh_events = {fk.Damage, fk.AfterTurnEnd},
  can_refresh = function(self, event, target, player, data)
    if target == player and player:getMark("lingling__fenzhi") > 0 then
      if event == fk.Damage then
        return true
      elseif event == fk.AfterTurnEnd then
        return player:getMark("lingling__fenzhi-turn") == 0
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "lingling__fenzhi", 0)
    for _, id in ipairs(player:getCardIds("h")) do
      room:setCardMark(Fk:getCardById(id), "lingling__fenzhi-inhand", 0)
    end
    player:filterHandcards()
  end,
}
local fenzhi_filter = fk.CreateFilterSkill{
  name = "#lingling__fenzhi_filter",
  anim_type = "offensive",
  card_filter = function(self, card, player)
    return player:getMark("lingling__fenzhi") > 0 and table.contains(player:getCardIds("h"), card.id) and
      card.color == Card.Red and card:getMark("lingling__fenzhi-inhand") == 0
  end,
  view_as = function(self, card)
    return Fk:cloneCard("fire_attack", card.suit, card.number)
  end,
}
local daishi = fk.CreateTriggerSkill{
  name = "lingling__daishi",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseChanging},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to == Player.Discard
  end,
  on_use = Util.TrueFunc,
}
fenzhi:addRelatedSkill(fenzhi_delay)
fenzhi:addRelatedSkill(fenzhi_filter)
goujian:addSkill(fenzhi)
goujian:addSkill(daishi)
Fk:loadTranslationTable{
  ["lingling__goujian"] = "勾践",
  ["#lingling__goujian"] = "吞江瞵海",
  ["illustrator:lingling__goujian"] = "珊瑚虫",
  ["designer:lingling__goujian"] = "伶",

  ["lingling__fenzhi"] = "焚志",
  [":lingling__fenzhi"] = "出牌阶段限一次，你可以令一名角色摸两张牌，其选择一张手牌，则其除此牌以外的红色手牌皆视为【火攻】直到其造成伤害后"..
  "或其下回合结束后。",
  ["lingling__daishi"] = "待时",
  [":lingling__daishi"] = "你跳过弃牌阶段。"..
  "<br><br> <font color = '#a40000'>苦心人，天不负，卧薪尝胆，三千越甲可吞吴。",
  ["#lingling__fenzhi"] = "焚志：令一名角色摸两张牌，其选择一张手牌，除此牌外红色手牌均视为【火攻】",
  ["#lingling__fenzhi-ask"] = "焚志：请选择一张手牌，除此牌外红色手牌均视为【火攻】直到你造成伤害或下回合开始",
  ["#lingling__fenzhi_filter"] = "焚志",
}

return extension
